
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 0154
 * @Author : WangRuoyu 
 * @Date : 2023/5/20 17:04
 */

#include "iostream"

using namespace std;

int main() {
    int n, k;
    scanf("%d %d", &n, &k);

    int a[n];
    int q[n + 10];
    int h = 0;
    int t = 0;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
    }

    for (int i = 0; i < n; ++i) {
        while (h < t && a[i] <= a[q[t - 1]]) {
            t--;
        }
        q[t++] = i;
        if (q[h] <= i - k) {
            h++;
        }
        if (i >= k - 1) {
            printf("%d ", a[q[h]]);
        }
    }

    puts("");

    h = 0;
    t = 0;
    for (int i = 0; i < n; ++i) {
        while (h < t && a[i] >= a[q[t - 1]]) {
            t--;
        }
        q[t++] = i;
        if (q[h] <= i - k) {
            h++;
        }
        if (i >= k - 1) {
            printf("%d ", a[q[h]]);
        }
    }

    return 0;
}